<?php

namespace app\admin\service;

use app\common\Model\OrderActionShaohuo;
use app\common\Model\OrderInfoShaohuo;
use app\common\Model\User;
use app\common\Service;
use think\Config;

class ShaohuoOrderService extends Service
{
    //操作类型对应描述信息
    private $action_type = [
        '1' => '[name1] 提交了订单',
        '2' => '[name1] 已完成付款',
        '3' => '[name1] 取消了订单',
        '4' => '捎货人 [name2]([phone2])已接单，请耐心等待取货',
        '5' => '捎货人 [name2]([phone2])已取件，正在送货途中，请准备收货',
        '6' => '捎货人 [name2]([phone2])已完成送货，收件人[name3]',
        '7' => '[name1] 已确认签收，感谢您使用捎货，期待再次为您服务',
        '9' => '[name1] 将订单恢复自由接单'
    ];

    /**
     * 生成订单路由信息 在上层事务内执行
     * @param $order_id
     * @param $type 操作类型1提交订单2完成付款3取消订单4接单5取件6已送达7已签收(完成)
     * @param $deal_uid
     * @param string $commit
     */
    public function orderAction($order_id,$type,$deal_uid,$commit = '')
    {
        //查询用户信息
        $deal_user = User::get($deal_uid);

        //操作人 优先真实姓名 其次昵称 再次 手机号
        if ($deal_user) {
            $name = ($name = $deal_user->real_name ? $deal_user->real_name : $deal_user ->nick_name) ? $name : $deal_user->mobile;
        }else{
            $name = '';
        }

        $name1 = '';
        $name2 = '';
        $phone2 = '';
        $name3 = '';

        if(in_array($type,[1,2,3,7,9])){
            $name1 = $name;
        }else{
            $name2 = $name;
            $phone2 = $deal_user->mobile;
            $name3 = $commit;
        }

        $desc = str_replace('[name1]',$name1,$this->action_type[$type]);
        $desc = str_replace('[name2]',$name2,$desc);
        $desc = str_replace('[phone2]',$phone2,$desc);
        $desc = str_replace('[name3]',$name3,$desc);

        //保存信息
        $data = [
            'order_id' => $order_id,
            'type' => $type,
            'desc' => $desc,
            'deal_uid' => $deal_uid,
            'commit' => $commit
        ];

        $rtn = OrderActionShaohuo::get([
            'order_id' => $order_id,
            'type' => $type,
            'deal_uid' => $deal_uid,
        ]);
        if(empty($rtn)){
            OrderActionShaohuo::create($data);
        }
    }
}